<?php

/**
 * 违禁词相关Model 
 */
class badwordsModel
{

    /**
     * 违禁词列表数据
     * @var array 
     */
    public $replace = array();
            
    /**
     * Base
     * @var object 
     */
    public $base;

    /**
     * Redis 连接
     * @var resources 
     */
    private $redis;

    public function __construct(& $base)
    {
        $this->base = & $base;
        $this->redis = & $this->base->redis;

        // 载入 wenlin's Badwords 库
        $this->base->loadLib('badwords', array(
            'replace' => $this->getBadWordsData(),
            'triebin' => '/tmp/com.hupu.games-wordsfb.bin',
            'persistkey' => 'badwords::com.hupu.games::wordsfb',
        ));
    }

    /**
     * 检测内容中是否存在违禁词，如果存在则返回该违禁词
     * @param string $content
     * @return string 
     */
    public function check($content)
    {
        $result = NULL;
        $badWords = $this->getBadWords();
  
        foreach($badWords as $key => & $val)
        {
           if(strpos($content, (string) $val) !== FALSE)
           {
               $result = $val;

               break;
           }
        }
        //ljj todo 做成 event
        if($result){
            $this->base->load('log')->logBadwords($result);
        }
        return $result;
        // return $_ENV['badwordsLib']->check(& $content);
    }

    public function checkUserName($userName)
    {
        $status = FALSE;
        $badWords = $this->getUserNameBadWords();

        foreach($badWords as $key => & $val)
        {
            if(stripos($userName, (string) $val) !== FALSE)
            {
                $status = $val;

                break;
            }
        }

        return $status;
    }

    public function getUserNameBadWords()
    {
        $badWords = $this->redis->hget(REDIS_PREFIX . 'Cache', 'username_badwords');
        $badWords && $badWords = explode('|', $badWords);
        !$badWords && $badWords = [];

        return $badWords;
    }

    public function setUserNameBadWords(array $badWords)
    {
        $badWords = array_filter($badWords);
        $badWords = array_unique($badWords);

        return $this->redis->hset(REDIS_PREFIX . 'Cache', 'username_badwords', implode('|', $badWords)) !== FALSE;
    }

    /**
     * 替换内容中的违禁词
     * @param string $content
     * @return string 
     */
    public function replace($content)
    {
        var_dump($_ENV['badwordsLib']->replace($content));
        exit;
        return $_ENV['badwordsLib']->replace($content);
    }

    /**
     * 获取违禁词列表数据
     * @return array 
     */
    public function getBadWords()
    {
        $badWords = array();

        $replace = $this->getBadWordsData();
        if(!empty($replace['words']))
        {
            foreach($replace['words'] as $k => $v)
            {
                $badWords[] = $k . ($v ? '=>' . $v : '');
            }

            unset($replace);
        }

        return $badWords;
    }
    
    /**
     * 获取违禁词列表原数据
     * @return array
     */
    public function getBadWordsData()
    {
        if(empty($this->replace))
        {
            $this->replace = $this->redis->hget(REDIS_PREFIX . 'Cache', 'badwords');
            $this->replace && $this->replace = @json_decode($this->replace, TRUE);
            !$this->replace && $this->replace = array('uptime' => '', 'words' => array());
        }
        
        return $this->replace;
    }

    /**
     * 设置违禁词列表数据
     * @param array $data
     * @return boolean 
     */
    public function setBadWords(array $data)
    {
        $result = FALSE;

        if($data)
        {
            $replace = array(
                'uptime' => $this->base->time,
                'words' => array(),
            );

            foreach($data as $k => $v)
            {
                $vs = explode('=>', $v);
                $replace['words'][$vs[0]] = !empty($vs[1]) ? $vs[1] : '';
            }

            $result = $this->redis->hset(REDIS_PREFIX . 'Cache', 'badwords', json_encode($replace)) !== FALSE;
        }

        return $result;
    }

}